package cn.oveay.classdesign.controller.servlet;

import cn.oveay.classdesign.bean.basebean.Subject;
import cn.oveay.classdesign.service.SubjectService;
import cn.oveay.classdesign.service.impl.SubjectServiceImpl;
import cn.oveay.classdesign.util.commons.DateFormatter;
import cn.oveay.classdesign.util.commons.exception.DateFormatException;
import cn.oveay.classdesign.util.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet("/list/modify")
public class ModifyServlet extends BaseServlet {
    private SubjectService subjectService = new SubjectServiceImpl();
    public String forward(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Subject> list = subjectService.findAll();
        req.setAttribute("subjectList", list);
        req.setAttribute("currdate", new Date().getTime());
        return "/WEB-INF/pages/modify.jsp";
    }

    public String forwardModify(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String subjectId = req.getParameter("id");
        Subject subject = subjectService.findById(subjectId);
        subject.setStartTimeView(subject.getStartTimeView().replace(' ', 'T').substring(0, 16));
        subject.setEndTimeView(subject.getEndTimeView().replace(' ', 'T').substring(0, 16));
        req.setAttribute("subject", subject);
//        req.setAttribute("currdate", new Date().getTime());
        return "/WEB-INF/pages/modifyPage.jsp";
    }

    public String modify(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String subjectId = req.getParameter("id");
        Subject subject = subjectService.findById(subjectId);
        subject.setStartTimeView(subject.getStartTimeView().replace(' ', 'T').substring(0, 16));
        subject.setEndTimeView(subject.getEndTimeView().replace(' ', 'T').substring(0, 16));
        String title = req.getParameter("title");
        Long endTime = null;
        Long startTime = null;
        if(title.trim().equals("")){
            subject.setTitle(title);
            req.setAttribute("subject", subject);
            req.setAttribute("error", "必须填写标题.");
            return "/WEB-INF/pages/modifyPage.jsp";
        }
        try {
            endTime = DateFormatter.formatToTimestamp(req.getParameter("endDate").replace('T', ' ') + ":00");
        } catch (DateFormatException e) {
            subject.setTitle(title);
            subject.setEndTimeView(req.getParameter("endDate"));
            req.setAttribute("subject", subject);
            req.setAttribute("error", "结束时间必须填写.");
            return "/WEB-INF/pages/modifyPage.jsp";
        }
        try {
            startTime = DateFormatter.formatToTimestamp(req.getParameter("startDate").replace('T', ' ') + ":00");
        } catch (DateFormatException e) {
            subject.setTitle(title);
            subject.setEndTimeView(req.getParameter("endDate"));
            subject.setStartTimeView(req.getParameter("startDate"));
            req.setAttribute("subject", subject);
            req.setAttribute("error", "开始时间必须填写.");
            return "/WEB-INF/pages/modifyPage.jsp";
        }
        if(endTime < startTime){
            subject.setTitle(title);
            subject.setEndTimeView(req.getParameter("endDate"));
            subject.setStartTimeView(req.getParameter("startDate"));
            req.setAttribute("subject", subject);
            req.setAttribute("error", "时间必须是开始时间未来的时间.");
            return "/WEB-INF/pages/modifyPage.jsp";
        }
        subject.setTitle(title);
        subject.setStarttime(startTime);
        subject.setEndtime(endTime);
        subjectService.update(subject);
        return "r:/list/modify?method=forward";
    }

    public String delete(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        subjectService.delete(id);
        System.out.println("delete Subject");
        return "r:/list/modify?method=forward";
    }
}
